package com.four.reimbursemanage.controller;

import com.four.reimbursemanage.common.bean.ResponseData;
import com.four.reimbursemanage.common.controller.BaseController;
import com.four.reimbursemanage.common.util.AliOssUntil;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

@RestController
@CrossOrigin
public class FileUploadController extends BaseController {
    @PostMapping("/upload")
    public ResponseData<Object> upload(MultipartFile file) throws IOException {
        //把文件内容存储在本地磁盘上
        String orignalFilename = file.getOriginalFilename();
        //保证文件的名字是唯一的，防止文件覆盖
        String filename = UUID.randomUUID().toString()+orignalFilename.substring(orignalFilename.lastIndexOf("."));
       // file.transferTo(new File("C:\\Users\\lwy\\Desktop\\files\\"+filename));
        String url = AliOssUntil.uploadFile(filename,file.getInputStream());
        return returnObject(url);

    }
}
